package com.fs.base.vo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import lombok.Data;
import lombok.NoArgsConstructor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.AfterFilter;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.fs.base.annotation.EasyUiComboBox;
import com.fs.base.pojo.PkEntity;
import com.fs.base.utils.ModleTools;
import com.fs.base.utils.StringTools;

/**
 * @author TiddlerCJ
 */

@Data
@NoArgsConstructor
public class Combobox {
	private final static String COMBOBOX_TEXTFIELD = "text";
	private final static String COMBOBOX_VALUEFIELD = "value";
	/*
	 * 当前实体类名
	 */
	@JSONField(serialize=false)
	private String className;
	/*
	 * 默认文本值
	 */
	@JSONField(serialize=false)
	private String text;
	
	/*
	 * 默认值域文本
	 */
	@JSONField(serialize=false)
	private String value;
	/*
	 * 值域
	 */
	@JSONField(serialize=false)
	private String valueField ;
	/*
	 * 文本域
	 */
	@JSONField(serialize=false)
	private String textField;
	/*
	 * 附加属性 多个属性用,号分隔如：dicName,dicValue
	 */
	@JSONField(serialize=false)
	private String attributes;
	
	public Combobox(JSONArray data){
		this.data = data;
	}
	
	private JSONArray data = new JSONArray();
	
	/**
	 * 构建combobox对象
	 * @param list
	 * @param combobox
	 * @return
	 */
	public static <K> Combobox builder(List<K> list,Combobox combobox){
		String className = combobox.getClassName();
		List<K> rows = list;
		String text = combobox.getText();
		final String value = combobox.getValue();
		String valueField = combobox.getValueField();
		String textField = combobox.getTextField();
		String attr = combobox.getAttributes();
		
		EasyUiComboBox easyUiComboBox = null;
		if(CollectionUtils.isEmpty(rows)) rows = new ArrayList<K>();
		String groupField = null; 
		/*
		 * 利用反射获取当前实体的EasyUiComboBox
		 */
		try {
			//easyUiComboBox = Class.forName(className).getAnnotation(EasyUiComboBox.class);
			easyUiComboBox = (EasyUiComboBox) ModleTools.getAnnotationByName(className, EasyUiComboBox.class);
			if(easyUiComboBox!=null){
				if(StringUtils.isBlank(valueField)){
					valueField = easyUiComboBox.valueField();
				}
				if(StringUtils.isBlank(textField)){
					textField = easyUiComboBox.textField();
				}
				groupField = easyUiComboBox.groupField();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(StringUtils.isBlank(valueField)) valueField = PkEntity.PRIMARY_KEY_NAME;
		if(StringUtils.isBlank(textField)) textField = COMBOBOX_TEXTFIELD;
		
		Set<String> fieldSet = new HashSet<String>();
		fieldSet.add(valueField);
		fieldSet.add(textField);
		
		if(StringUtils.isNotBlank(groupField)){
			fieldSet.add(groupField);
		}
		List<String> attrList = StringTools.toList(attr, ","); 
		if(attrList!=null) fieldSet.addAll(attrList);
		
		final Boolean defaultNull = (text==null);
		final String _textField = textField;
		final String _valueField = valueField;
		final String _defaultText = defaultNull ? "" : text;
		final List<String> defualtList = StringTools.toList(_defaultText, ",");
		
		//默认value值，多个，号分隔
		final List<String> defValueList = StringTools.toList((value+""), ","); 
		/*
		 * 默认选中
		 */
		AfterFilter afterFilter =new AfterFilter() {
			public void writeAfter(Object object) {
				JSONObject item = (JSONObject) JSON.toJSON(object);
				if(!defaultNull && _textField!=null){
					if(defualtList.contains(item.get(_textField))){
						writeKeyValue("selected", true);
						writeKeyValue("checked", true);
					}
				}
				
				if(StringUtils.isNotBlank(value) && defValueList.contains(item.get(_valueField))){
					writeKeyValue("selected", true);
					writeKeyValue("checked", true);
				}
				
				if(!item.containsKey(Combobox.COMBOBOX_VALUEFIELD)){
					writeKeyValue(Combobox.COMBOBOX_VALUEFIELD, item.get(_valueField));
				}
				if(!item.containsKey(Combobox.COMBOBOX_TEXTFIELD)){
					writeKeyValue(Combobox.COMBOBOX_TEXTFIELD, item.get(_textField));
				}
			}
		};
		
		/*
		 *修改属性名称 
		*/
		/*NameFilter nameFilter = new NameFilter() {
			public String process(Object object, String name, Object value) {
				if(_textField.equals(name)){
					return Combobox.COMBOBOX_TEXTFIELD;
				}
				if(_valueField.equals(name)){
					return Combobox.COMBOBOX_VALUEFIELD;
				}
				return name;
			}
		};*/
		 
		
		String[] properties = fieldSet.toArray(new String[]{});
		/*
		 * 字段过滤
		 */
		SimplePropertyPreFilter filter = new SimplePropertyPreFilter(properties); 
		SerializeWriter sw = new SerializeWriter(SerializerFeature.DisableCircularReferenceDetect);  
        JSONSerializer serializer = new JSONSerializer(sw);   
       /* if(easyUiComboBox!=null){
        	serializer.addFilter(nameFilter);
        }*/
        serializer.addFilter(filter);
        serializer.addFilter(afterFilter);
        serializer.write(rows);    
		return new Combobox(JSONArray.parseArray(sw.toString()));
	}
	
}
